package com.xncoding.pos.service.thread;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.concurrent.CompletableFuture;

/**
 * 异步实现接口
 *
 * @Author guoyb
 * @Date 2021/12/10  15:22
 */
@Slf4j
@Service
public class CompletableFutureService {

    // 需要注意的是： 异步方法使用注解@Async的返回值只能为void或者Future及其子类，当返回结果为其他类型时，方法还是会异步执行，但是返回值都是null，

    @Async
    public CompletableFuture<String> doSomething1(String message) throws InterruptedException {
        log.info("do something1: {}", message);
        Thread.sleep(1000);
        return CompletableFuture.completedFuture("do something1: " + message);
    }

    @Async
    public CompletableFuture<String> doSomething2(String message) throws InterruptedException {
        log.info("do something2: {}", message);
        Thread.sleep(2000);
        return CompletableFuture.completedFuture("; do something2: " + message);
    }

    @Async
    public CompletableFuture<String> doSomething3(String message) throws InterruptedException {
        log.info("do something3: {}", message);
        Thread.sleep(3000);
        return CompletableFuture.completedFuture("; do something3: " + message);
    }

}
